/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;
import DAO.TinTucDAO;
import DTO.TinTucDTO;
import Threads.updateImage;
import java.awt.Desktop;
import java.awt.Font;
import java.awt.GridLayout;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.text.SimpleDateFormat;
import javax.swing.JOptionPane;


/**
 *
 * @author QuangHau
 */
public class pnNews extends javax.swing.JPanel {

    /**
     * Creates new form pnNews
     */
    private TinTucDTO news;
    public pnNews(TinTucDTO _news, int status) {
        initComponents();
        news = _news;
        pnTitle.setLayout(new GridLayout(1, 1));
        pnDescription.setLayout(new GridLayout(1, 1));
        jScrollPane1.setBorder(null);
        jScrollPane3.setBorder(null);
        txtTitle.setText(news.getTieuDe());
        SimpleDateFormat ft = new SimpleDateFormat("dd/MM/yyyy HH:mm");
        lbNgayDang.setText(ft.format(news.getNgayDang()) + " | " + news.getTenWebsite() + ">" + news.getTenChuDe());
        txtDescription.setText(news.getMoTa());
        lbXoa.setToolTipText("Xóa tin " + news.getTieuDe());
        lbXem.setToolTipText(news.getLink());

        if (status == 0) {
            lbXoa.setVisible(false);
        } else {
            lbXoa.setVisible(true);
        }
        updateImage udi = new updateImage(lbImage, news.getMaTinTuc(), news.getHinhAnh());
        udi.start();
    }

    private void openNew(){
        try {
            Desktop.getDesktop().browse(new URL(news.getLink()).toURI());
        } catch (MalformedURLException ex) {
            //Logger.getLogger(pnNews.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, "Không thể mở", "Lỗi", JOptionPane.ERROR_MESSAGE);
        } catch (IOException | URISyntaxException ex) {
            //Logger.getLogger(pnNews.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, "Không thể mở", "Lỗi", JOptionPane.ERROR_MESSAGE);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        lbImage = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        pnTitle = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtTitle = new javax.swing.JTextArea();
        lbNgayDang = new javax.swing.JLabel();
        pnDescription = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        txtDescription = new javax.swing.JTextArea();
        lbXoa = new javax.swing.JLabel();
        lbXem = new javax.swing.JLabel();

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane2.setViewportView(jTextArea1);

        setBackground(new java.awt.Color(255, 255, 255));
        setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        lbImage.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lbImage.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Picture/no_image.jpg"))); // NOI18N
        lbImage.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        txtTitle.setEditable(false);
        txtTitle.setColumns(20);
        txtTitle.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        txtTitle.setLineWrap(true);
        txtTitle.setRows(1);
        txtTitle.setWrapStyleWord(true);
        txtTitle.setBorder(null);
        txtTitle.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        txtTitle.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtTitleMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(txtTitle);

        javax.swing.GroupLayout pnTitleLayout = new javax.swing.GroupLayout(pnTitle);
        pnTitle.setLayout(pnTitleLayout);
        pnTitleLayout.setHorizontalGroup(
            pnTitleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnTitleLayout.createSequentialGroup()
                .addGap(0, 0, 0)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 256, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0))
        );
        pnTitleLayout.setVerticalGroup(
            pnTitleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnTitleLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0))
        );

        lbNgayDang.setFont(new java.awt.Font("Arial", 0, 13)); // NOI18N
        lbNgayDang.setText("jLabel4");

        pnDescription.setBackground(new java.awt.Color(255, 255, 255));

        txtDescription.setEditable(false);
        txtDescription.setColumns(20);
        txtDescription.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        txtDescription.setLineWrap(true);
        txtDescription.setRows(1);
        txtDescription.setWrapStyleWord(true);
        jScrollPane3.setViewportView(txtDescription);

        javax.swing.GroupLayout pnDescriptionLayout = new javax.swing.GroupLayout(pnDescription);
        pnDescription.setLayout(pnDescriptionLayout);
        pnDescriptionLayout.setHorizontalGroup(
            pnDescriptionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.TRAILING)
        );
        pnDescriptionLayout.setVerticalGroup(
            pnDescriptionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnDescriptionLayout.createSequentialGroup()
                .addGap(0, 0, 0)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnTitle, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(lbNgayDang)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addComponent(pnDescription, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(0, 0, 0)
                .addComponent(pnTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lbNgayDang)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnDescription, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(0, 0, 0))
        );

        lbXoa.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbXoa.setText("Xóa");
        lbXoa.setToolTipText("Xóa tin");
        lbXoa.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        lbXoa.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lbXoaMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                lbXoaMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                lbXoaMouseExited(evt);
            }
        });

        lbXem.setText("Xem");
        lbXem.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        lbXem.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lbXemMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                lbXemMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                lbXemMouseExited(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lbXem)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lbXoa))
                    .addComponent(lbImage, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(5, 5, 5))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lbImage, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lbXoa)
                            .addComponent(lbXem))
                        .addGap(0, 10, Short.MAX_VALUE))))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void lbXoaMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lbXoaMouseEntered
        lbXoa.setFont(new Font("Tahoma", 1, 11));
    }//GEN-LAST:event_lbXoaMouseEntered

    private void lbXoaMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lbXoaMouseExited
        lbXoa.setFont(new Font("Tahoma", 0, 11));
    }//GEN-LAST:event_lbXoaMouseExited

    private void lbXoaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lbXoaMouseClicked
        int res = JOptionPane.showConfirmDialog(null,"Xóa tin " + txtTitle.getText()+  "?","Xóa tin tức", JOptionPane.YES_NO_OPTION);
        if(res == JOptionPane.YES_OPTION){
            if(TinTucDAO.DeleteTinTuc(news.getMaTinTuc())){
                File f = new File("Image\\" + news.getMaTinTuc() + ".jpg");
                if(f.exists()){
                    f.delete();
                }
                JOptionPane.showMessageDialog(null, "Xóa thành công", "Thông Tin", JOptionPane.INFORMATION_MESSAGE);
                this.setVisible(false);
            }
            else{
                JOptionPane.showMessageDialog(null, "Xóa Tthất bại", "Thông Tin", JOptionPane.INFORMATION_MESSAGE);
            }
        }
    }//GEN-LAST:event_lbXoaMouseClicked

    private void lbXemMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lbXemMouseExited
        lbXem.setFont(new Font("Tahoma", 0, 11));
    }//GEN-LAST:event_lbXemMouseExited

    private void lbXemMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lbXemMouseEntered
        lbXem.setFont(new Font("Tahoma", 1, 11));
    }//GEN-LAST:event_lbXemMouseEntered

    private void lbXemMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lbXemMouseClicked
        openNew();
    }//GEN-LAST:event_lbXemMouseClicked

    private void txtTitleMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtTitleMouseClicked
        openNew();
    }//GEN-LAST:event_txtTitleMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JLabel lbImage;
    private javax.swing.JLabel lbNgayDang;
    private javax.swing.JLabel lbXem;
    private javax.swing.JLabel lbXoa;
    private javax.swing.JPanel pnDescription;
    private javax.swing.JPanel pnTitle;
    private javax.swing.JTextArea txtDescription;
    private javax.swing.JTextArea txtTitle;
    // End of variables declaration//GEN-END:variables

//   public static void main(String args[]) {
//       pnNews a = new pnNews();
//       JFrame ab= new JFrame();
//       ab.setContentPane(a);
//       ab.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
//       ab.setVisible(true);
//   }
}
